#include <allegro.h>
#include <stdio.h>
#include "gentel.h"




BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	/*if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}*/
	return (Background);

}


void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}


/*
 * module gentel
 */

void load_gentel(gentel *gentel)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "right%d.bmp", i);
		gentel->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "left%d.bmp", i);
		gentel->img_left[i] = load_bitmap(buf, NULL);
	}
}


void destroy_gentel(gentel *gentel)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(gentel->img_right[i]);
		destroy_bitmap(gentel->img_left[i]);
	}

}
void init_gentel(gentel *gentel,cercle *cercle,BITMAP*Buffer)
{
	gentel->direction = 2;
	gentel->x = 720;
	gentel->y = 480;
}
void move_gentel_right(gentel *gentel,int *compteur,BITMAP *Background,BITMAP *Buffer)
{

	if (key[KEY_RIGHT] /*&& (gentel->x < SCREEN_W - gentel->img_right[0]->w)*/) {
		*compteur+=0.5;

		(gentel->x)+=1;
		gentel->direction = 0; //right
	}

}
void move_gentel_left(gentel *gentel,int *compteur,BITMAP * Background,BITMAP*Buffer)
{



	if (key[KEY_LEFT] && (gentel->x >= 0))
 {
		*compteur+=0.5;

		(gentel->x)-=1;
		gentel->direction = 1; //left
	}
}

void jump_gentel(gentel *gentel,BITMAP * Background)
{

	if (key[KEY_UP] && (gentel->y >0) ) 
        {
		(gentel->y)-=2;
		gentel->direction = 2; //jump
	}


}

void gravity_gentel(gentel *gentel,BITMAP * Background)
{
int i,x,y;
x = (gentel->x);
y=(gentel->y);
i = getpixel(Background,x+38,y+75);
printf("%d \n",i);
if (i!=16777215 )
{
	(gentel->y)+=1;
	gentel->direction = 2;
//printf("%d \n",gentel->y);
}

}
void move_gentel(gentel *gentel,int *compteur,BITMAP * Background,BITMAP *Buffer)
{
	move_gentel_right(gentel,compteur,Background,Buffer);
	move_gentel_left(gentel,compteur,Background,Buffer);
	jump_gentel(gentel,Background);
         

         gravity_gentel(gentel, Background);

	//add gravity
}
void draw_gentel(gentel gentel,BITMAP *Buffer, int compteur)
{
	static int last_direction;

	if (gentel.direction == 0){ //moving right

		draw_sprite(Buffer, gentel.img_right[compteur % (NB_IMAGES-1) ],gentel.x, gentel.y);
		last_direction=0;
	}

	if(gentel.direction == 1){ //moving left

		draw_sprite(Buffer, gentel.img_left[compteur % (NB_IMAGES-1) ],gentel.x, gentel.y);
		last_direction=1;
	}
	if (gentel.direction == 2 && last_direction==0) {	//jumping right

		draw_sprite(Buffer, gentel.img_right[NB_IMAGES - 1],gentel.x, gentel.y);
	}
	if (gentel.direction == 2 && last_direction==1) {	//jumping left

		draw_sprite(Buffer, gentel.img_left[NB_IMAGES - 1],gentel.x, gentel.y);
	}
}


void tir_gentel(BITMAP* Buffer, gentel* gentel,boull* boull, int* tir_status)
{


	if((key[KEY_A]))
	{

*tir_status += 1;
boull->direction = gentel->direction;	

if (*tir_status=0)
{
boull->direction = gentel->direction;
boull->bx = gentel->x+50;
boull->by = gentel->y+40;

}
if (*tir_status=1)
{
boull->direction = gentel->direction;
boull->bx = gentel->x+50;
boull->by = gentel->y+40;
}}
	
	


}

  //module collision
int collision(gentel *gentel,piece *piece,int score)
{

    int sprite1_x= piece->px;
    int sprite1_y= piece->py;
    int sprite1_w= piece->px + 33 - sprite1_x;//
    int sprite1_h= piece->py + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))  
	{
        //destroy_bitmap(piece->imagep);
	piece->px=900;
	piece->py=900;
score=score+10; return score;
//printf("collision");
    }
return score;
   


    
}
int collision1(gentel *gentel,piece1 *piece1,int score)
{
	int x ;
    int sprite1_x= piece1->p1x;
    int sprite1_y= piece1->p1y;
    int sprite1_w= piece1->p1x + 33- sprite1_x;//
    int sprite1_h= piece1->p1y + 40- sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +77 ;//
    int sprite2_h= gentel->y + 90;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))   
	{
        //destroy_bitmap(piece->imagep);
	piece1->p1x=900;
	piece1->p1y=900;
score=score+10; return score;
//printf("collision");
    }
   


    return score;
}

int collision2(gentel *gentel,piece2 *piece2,int score)
{

    int sprite1_x= piece2->p2x;
    int sprite1_y= piece2->p2y;
    int sprite1_w= piece2->p2x + 33 - sprite1_x;//
    int sprite1_h= piece2->p2y + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +77 ;//
    int sprite2_h= gentel->y + 100;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))   
	{
        //destroy_bitmap(piece->imagep);
	piece2->p2x=900;
	piece2->p2y=900;
score=score+10; return score ;
//printf("collision");
    }
   


    return score;
}

int collision3(gentel *gentel,piece3 *piece3,int score)
{

    int sprite1_x= piece3->p3x;
    int sprite1_y= piece3->p3y;
    int sprite1_w= piece3->p3x + 33 - sprite1_x;//
    int sprite1_h= piece3->p3y + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))   
	{
        //destroy_bitmap(piece->imagep);
	piece3->p3x=900;
	piece3->p3y=900;
score=score+10; return score;
//printf("collision");
    }
   


    return score;
}

int collision4(gentel *gentel,piece4 *piece4,int score)
{

    int sprite1_x= piece4->p4x;
    int sprite1_y= piece4->p4y;
    int sprite1_w= piece4->p4x + 33 - sprite1_x;//
    int sprite1_h= piece4->p4y + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))    
	{
        //destroy_bitmap(piece->imagep);
	piece4->p4x=900;
	piece4->p4y=900;
score=score+10; return score ;
//printf("collision");
    }
   


    return score;
}

int collision5(gentel *gentel,piece5 *piece5,int score)
{

    int sprite1_x= piece5->p5x;
    int sprite1_y= piece5->p5y;
    int sprite1_w= piece5->p5x + 33 - sprite1_x;//
    int sprite1_h= piece5->p5y + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))   
	{
        //destroy_bitmap(piece->imagep);
	piece5->p5x=900;
	piece5->p5y=900;
score=score+10; return score;
//printf("collision");
    }
   


    return score;
}


int collision6(gentel *gentel,coeur *coeur,int score)
{

    int sprite1_x= coeur->cx;
    int sprite1_y= coeur->cy;
    int sprite1_w= coeur->cx + 33 - sprite1_x;//
    int sprite1_h= coeur->cy + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))    
	{
        //destroy_bitmap(piece->imagep);
	coeur->cx=900;
	coeur->cy=900;
score=score+50; return score ;
//printf("collision");
    }
   

return score;
    
}

int collision7(gentel *gentel,cle *cle)

{

    int sprite1_x= cle->cx;
    int sprite1_y= cle->cy;
    int sprite1_w= cle->cx + 33 - sprite1_x;//
    int sprite1_h= cle->cy + 40 - sprite1_y;//

    int sprite2_x= gentel->x;
    int sprite2_y= gentel->y;
    int sprite2_w= gentel->x +90 ;//
    int sprite2_h= gentel->y + 120;//
    
    /*if (bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,
                                sprite2_x, sprite2_y, sprite2_w, sprite2_h) )
*/
	if((sprite2_x<=sprite1_x+(sprite1_w/2)) && (sprite2_y>sprite1_y) && (sprite2_y< (sprite1_y+ sprite1_h))&& (sprite2_y>sprite1_y))
	{
        //destroy_bitmap(piece->imagep);
	cle->cx=900;
	cle->cy=900;
return 1;
//printf("collision");
    }
   

return 0;
    
}



int bounding_box_collision(int sprite1_x, int sprite1_y, int sprite1_w,
                           int sprite1_h, int sprite2_x, int sprite2_y, int sprite2_w,
                           int sprite2_h)
{
    if (sprite1_x > (sprite2_x + sprite2_w)  || sprite1_y > (sprite2_y + sprite2_h)||
            sprite2_x > (sprite1_x + sprite1_w)|| 
            sprite2_y > (sprite1_y + sprite1_h)) 
    {
        // no collision
        return 0;
    }

    // collision
    return 1;
}









/*int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}*/

